package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.imp.OnPositionTListener
import com.lv.service.R
import com.lv.service.adapter.RecyclerSelectedTagAdapter
import com.lv.service.adapter.RecyclerSelectedTagWhiteMatchAdapter
import com.lv.common.bean.CommonSelectedBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：底部公共list选择器
 */
class RecyclerSelectedTagTopHBottom52PopupView(
    private val mContext: Context,
    private val mAllBeans: List<CommonSelectedBean>? = null,
    private val mTitle: String,
    private val topH: Int = 0,
    private val mSelectedTag: Int = com.lv.common.R.drawable.ic_selected,//默认选中的样式
    private val mShowLastLine: Boolean = false,//是否显示最后一个条目的分割线
    private val mLayout: Int = R.layout.recycler_selected_tag_warp_bottom_52_popup,
    private val mPositionTListener: OnPositionTListener<CommonSelectedBean>? = null
) :
    BottomPopupView(mContext) {
    private var current = -1
    private var currentTemp = -1
    private val name = ""
    private var rvContent: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private var ivClose: ImageView? = null
    private val mBeans = mutableListOf<CommonSelectedBean>()

    private var mAdapter: RecyclerSelectedTagWhiteMatchAdapter? = null
    override fun getImplLayoutId(): Int {
        return mLayout
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        ivClose = findViewById<ImageView>(R.id.iv_close)
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initEvnet()
    }

    private fun initView() {
        mAllBeans?.let {
            mBeans.clear()
            mAllBeans.forEachIndexed { index, commonSelectedBean ->
                if (commonSelectedBean.selected) {
                    current = index
                    currentTemp = index
                }
            }
            mBeans.addAll(mAllBeans)
        }
        tvTitle?.text = mTitle

        val mLinearLayoutManager =
            LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        rvContent?.layoutManager = mLinearLayoutManager
        mAdapter = RecyclerSelectedTagWhiteMatchAdapter(mBeans, mSelectedTag, mShowLastLine)
        rvContent?.adapter = mAdapter
        if (currentTemp != -1) {
            rvContent?.scrollToPosition(currentTemp)
        }
        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            if (currentTemp == position) {
                return@OnPositionTListener
            }
            if (currentTemp != -1) {
                mBeans[currentTemp].selected = false
                mAdapter?.notifyItemChanged(currentTemp)
            }
            currentTemp = position
            mBeans[currentTemp].selected = true
            mAdapter?.notifyItemChanged(currentTemp)
//            mPositionTListener?.onClick(current, mBeans[current])
//            dismiss()
        }

    }


    private fun initEvnet() {
        viewTop?.setOnClickListener {
            if (mBeans.isNotEmpty()) {
                mBeans.forEach { it.selected = false }
                if (current >= 0) {
                    mBeans[current].selected = true
                }
            }
            dismiss()
        }
        ivClose?.setOnClickListener {
            if (mBeans.isNotEmpty()) {
                mBeans.forEach { it.selected = false }
                if (current >= 0) {
                    mBeans[current].selected = true
                }
            }
            dismiss()
        }

        viewShadow?.setOnClickListener {
            if (mBeans.isNotEmpty()) {
                current = currentTemp
                if (current >= 0) {
                    mPositionTListener?.onClick(current, mBeans[current])
                }
            }
            dismiss()
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}